package com.example.lishop.service.impl

import com.example.lishop.entity.BFileEntity
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.repository.FileRepository
import com.example.lishop.service.FileService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.util.Assert

/**
 * Created on 2021/3/14 18:14
 * @author shuguo
 */
@Service
class FileServiceImpl : FileService {

    @Autowired
    lateinit var repository: FileRepository

    override fun addFile(data: BFileEntity) {
        repository.save(data)
    }

    override fun delFile(fileId: String) {
        detailFile(fileId)
        repository.del(fileId)
    }

    override fun detailFile(fileId: String):BFileEntity? {
        val result = repository.findByFileId(fileId)
        Assert.notNull(result, ExceptEnum.NOT_EXIST)
        return result
    }

    override fun listFile(page: Int, size: Int): Page<BFileEntity> {
        val req: Pageable = PageRequest.of(page, size)
        return repository.findAll(req)
    }


}